<template>
  <div id="footer">
    <div class="footer-left flex-center">
      <span v-if="cartStore.total > 0"
        >￥{{ cartStore.totalAmount.toFixed(2) }}</span
      >
      <span v-else>未选购商品</span>
      <div class="round flex-center" @click="showPopup">
        <ShoppingCart style="width: 22px" />
        <div class="round-num flex-center" v-if="cartStore.total > 0">
          {{ cartStore.total }}
        </div>
      </div>
    </div>
    <div class="footer-right flex-center" :style="footerStyle">
      {{ cartStore.total == 0 ? "￥20起送" : "去结算" }}
    </div>
    <!-- Popup 组件 -->
    <van-popup v-model:show="show" position="bottom" :style="{ height: '45%' }">
      <div class="title">
        <div class="title-left">已选商品</div>
        <div class="title-right" @click="cartStore.clearCart()">清空</div>
      </div>
      <ul class="goods-list" v-for="item in cartStore.cartItems" :key="item.id">
        <li>
          <div class="good-name" style="color: black; width: 40%">
            {{ item.name }}
          </div>
          <div class="good-price" style="color: red; font-weight: 900">
            {{ item.price }}
          </div>
          <div class="good-count">
            <div class="minus" @click="cartStore.removeCartItem(item)">-</div>
            <span style="margin: 0 5px">{{ item.count }}</span>
            <div class="add" @click="cartStore.addCartItem(item)">+</div>
          </div>
        </li>
      </ul>
    </van-popup>
  </div>
</template>

<script setup>
import { Popup, Dialog } from "vant";
import { computed, ref } from "vue";
import { useCartStore } from "@/stores/cart.js"; // 引入 Pinia 仓库
const cartStore = useCartStore(); // 使用 Pinia 仓库

const clearCartWithConfirmation = () => {
  Dialog.confirm({
    title: "确认清空购物车？",
    message: "您确定要清空购物车吗？",
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(() => {
      // 用户点击确认，执行清空购物车的操作
      cartStore.clearCart();
    })
    .catch(() => {
      // 用户点击取消时执行的逻辑
      console.log("用户取消了清空购物车");
    });
};

// 定义一个响应式变量来控制 Popup 的显示
const show = ref(false);

// 点击事件处理函数
const showPopup = () => {
  show.value = true; // 改变变量的值来显示 Popup
};
// 计算属性，根据总价返回不同的样式对象
const footerStyle = computed(() => {
  return {
    "background-color": cartStore.total === 0 ? "rgb(50, 50, 50)" : "red",
    color: cartStore.total === 0 ? "rgb(143, 143, 143)" : "#fff",
  };
});
</script>

<style lang="less">
.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}
#footer {
  z-index: 99;
  font-size: 12px;
  width: 100%;
  height: 100%;
  color: rgb(143, 143, 143);
  background-color: #212121;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .footer-left {
    height: 100%;
    width: 60%;
    // background: rgb(116, 116, 116);
    .round {
      border: 4px solid #212121;
      width: 30px;
      height: 30px;
      border-radius: 50%;
      background: rgb(50, 50, 50);
      position: absolute;
      top: -10px;
      left: 20px;
      color: rgb(153, 153, 153);
      .round-num {
        font-size: 8px;
        position: absolute;
        top: -5px;
        right: -5px;
        width: 13px;
        height: 13px;
        border-radius: 50%;
        background: rgb(255, 0, 0);
        color: #fff;
      }
    }
  }
  .footer-right {
    height: 100%;
    width: 30%;
    background: rgb(50, 50, 50);
  }
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    background: #f6f6f6;
    height: 30px;
  }
  .goods-list {
    li {
      border-bottom: 1px solid #f4f4f4;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 10px;
      height: 40px;
      .good-count {
        width: 20%;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      .add,
      .minus {
        width: 15px;
        height: 15px;
        border-radius: 50%;
        background: rgb(255, 255, 255);
        color: #fff;
        text-align: center;
        line-height: 15px;
        font-size: 15px;
        border: 1px solid #ffdd48;
        background: #ffdd48;
      }
      .minus {
        line-height: 12px;
        font-size: 20px;
        background: #fff;
        color: #ffdd48;
      }
    }
  }
}
</style>
